<?php

/*	Vulpes-backend - Web-based managegent for wireless internet providers.
	Copyright (C) 2007 LinFox Serviços em Informática Ltda.

	This file is part of the Vulpes backend.

	Vulpes-backend is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Vulpes-backend is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

 	Please note that any front-end (client application) that is distributed
	along with the Vulpes back-end, such as the Vulpes front-end by LinFox,
	is subject to it's correspending licene. */

class client {
	public $username;
	public $name;
	public $address;
	public $district;
	public $postal_code;
	public $city;
	public $state;
	public $email;
	public $ip;
	public $mac;
	public $status;
	public $contracts;
	public $active = false;
	public $exists = false;
	public $key_username;
	private $password;
	private $db;
	protected $auth = false;

	function __construct($username = false) {
		global $db;
		$this->db =& $db;

		if ($username) {
			$this->username = $username;
			$this->key_username = $username;
			$sql_client = '
				SELECT `name`, `password`, `address`, `district`, `postal_code`, `city`, `state`, `email`, `status`, `last_access`, `active`
				FROM `clients`
				WHERE `username` = "'.$username.'"';
			$result_client = $this->db->query($sql_client);

			if ($result_client->num_rows > 0) {
				$client = $result_client->fetch_object();
				$result_client->free();
				foreach ($client as $key => $value) {
					$this->$key = $value;
				}
				$sql_contracts = '
					SELECT `contracts`.*, `plans`.`name` AS `plan_name`
					FROM `contracts`
					INNER JOIN `plans` ON (
						`plans`.`id` = `contracts`.`plan_id` )
					WHERE `client_username` = "'.$username.'"';
				$result_contracts = $this->db->query($sql_contracts);
				if ($result_contracts->num_rows > 0) {
					$this->contracts = array();
					$i = 0;
					while($this->contract[$i] = $result_contracts->fetch_object('contract'))
						$i++;
				}
				$result_contracts->free();
				$this->exists = true;
			}
		}
	}
	
	function verify()
	{
		if (!$this->exists) return false;
		if ($_POST['action'] == 'logout' || $_GET['action'] == 'logout') {
			$_SESSION = array();
			session_destroy();
			return false;
		} elseif (isset($_POST['client_username']) && isset($_POST['client_password']) && $_SESSION['vulpes_client_session'] != true) {

			if ($this->password == hash('sha512', $_POST['client_password'])) {
				$_SESSION['vulpes_client_session'] = true;
				$_SESSION['vulpes_client_username'] = $this->username;
				$_SESSION['vulpes_client_name'] = $this->name;
				$_SESSION['vulpes_client_email'] = $this->email;
			} else {
				return false;
			}
		}
		
		if ($_SESSION['vulpes_client_session'] == true) return true;
		return false;
	}
	
	function activate()
	{
		$timestamp_now = date('YmdHis');
		if(!$this->active) {
			$query_activate = '
				UPDATE `clients`
				SET `active` = 1,
					`last_access` = "'.$timestamp_now.'"
				WHERE `username` = "'.$this->username.'"';
			if ($this->db->query($query_activate)) {
				$this->active = true;
				exec('echo "'.$timestamp_now.': Activated client '.$this->username.'." >> /var/log/vulpes.log');	
				return true;
			}
		}
		return false;
	}

	function disactivate()
	{
		if($this->active) {
			$query_disactivate = 'UPDATE `clients` SET `active` = 0 WHERE `username` = "'.$this->username.'"';
			if ($this->db->query($query_disactivate)) {
				$this->active = false;
				return true;
			}
		}
		return false;
	}

	function info()
	{
		$result = '
			<client>
				<username>'.$this->username.'</username>
				<name>'.$this->name.'</name>
				<address>'.$this->address.'</address>
				<email>'.$this->email.'</email>
			</client>';
	}
	
	function contracts()
	{
		$result .= '<contracts>';
		if (is_array($this->contract)) {
			foreach ($this->contract as $contract) {
				$result .= xml_iterate($contract, 'contract');
			}
		}
		$result .= '</contracts>';
		
		return $result;
	}

	function insert()
	{
		global $page;

		$sql_check = '
			SELECT * FROM `clients`
				WHERE `username` = "' .$this->username. '"';
		$result_check = $this->db->query($sql_check);
		if ($result_check->num_rows > 0)
		{
			$page->error('{lang_user_allready_exists}');
			return false;
		}

		$sql_save = '
			INSERT INTO `clients`
			SET `username` = "' .$this->username. '",
				`password` = "' .$this->password. '",
				`name` = "' .$this->name. '",
				`address` = "' .$this->address. '",
				`district` = "' .$this->district. '",
				`postal_code` = "' .$this->postal_code. '",
				`city` = "' .$this->city. '",
				`state` = "' .$this->state. '",
				`email` = "' .$this->email. '"';
		if (!$this->db->query($sql_save)) {
			$page->error('{lang_changes_not_saved}');
			return false;
		} else {
			$page->notify('{lang_changes_saved}');
			return true;
		}
	}
	
	function update()
	{
		global $page;

		$sql_save = '
			UPDATE `clients`
			SET	`name` = "' .$this->name. '",
				`username` = "' .$this->username. '",
				`password` = "' .$this->password. '",
				`address` = "' .$this->address. '",
				`district` = "' .$this->district. '",
				`postal_code` = "' .$this->postal_code. '",
				`city` = "' .$this->city. '",
				`state` = "' .$this->state. '",
				`email` = "' .$this->email. '"
			WHERE `username` = "' .$this->key_username. '"';
		$sql_update_contracts = '
			UPDATE `contracts`
			SET `client_username` = "'.$this->username.'"
			WHERE `client_username` = "'.$this->key_username.'"';
		if (!$this->db->query($sql_save)) {
			$page->error('{lang_changes_not_saved}, '.$this->db->error);
			return false;
		} else {
			$this->db->query($sql_update_contracts);
			$page->notify('{lang_changes_saved}');
			return true;
		}
	}
	
	function set_password($password)
	{
		global $page;
		$this->password = hash('sha512', $password); 
		return true;
	}

}

?>
